home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / RCS / main.c,v < prev    next >
Encoding:
Text File  |  1992-05-28  |  47.7 KB  |  1,940 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     92.05.28.11.37.34;  author jhh;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     91.07.02.22.56.44;  author jhh;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     91.07.02.16.52.13;  author jhh;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @@
  27.  
  28.  
  29. 1.3
  30. log
  31. @changed prompt for kgdb to "kgdb"
  32. @
  33. text
  34. @/* Top level for GDB, the GNU debugger.
  35.    Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc.
  36.  
  37. This file is part of GDB.
  38.  
  39. GDB is free software; you can redistribute it and/or modify
  40. it under the terms of the GNU General Public License as published by
  41. the Free Software Foundation; either version 1, or (at your option)
  42. any later version.
  43.  
  44. GDB is distributed in the hope that it will be useful,
  45. but WITHOUT ANY WARRANTY; without even the implied warranty of
  46. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  47. GNU General Public License for more details.
  48.  
  49. You should have received a copy of the GNU General Public License
  50. along with GDB; see the file COPYING.  If not, write to
  51. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  52.  
  53. #include <stdio.h>
  54. #include "defs.h"
  55. #include "command.h"
  56. #include "param.h"
  57.  
  58. #ifdef USG
  59. #include <sys/types.h>
  60. #include <unistd.h>
  61. #endif
  62.  
  63. #include <sys/file.h>
  64. #include <setjmp.h>
  65. #include <signal.h>
  66. #include <sys/param.h>
  67. #include <sys/stat.h>
  68.  
  69. #ifdef SET_STACK_LIMIT_HUGE
  70. #include <sys/time.h>
  71. #include <sys/resource.h>
  72. #include <ctype.h>
  73.  
  74. int original_stack_limit;
  75. #endif
  76.  
  77. /* If this definition isn't overridden by the header files, assume
  78.    that isatty and fileno exist on this system.  */
  79. #ifndef ISATTY
  80. #define ISATTY(FP)    (isatty (fileno (FP)))
  81. #endif
  82.  
  83. extern void free ();
  84.  
  85. /* Version number of GDB, as a string.  */
  86.  
  87. extern char *version;
  88.  
  89. /*
  90.  * Declare all cmd_list_element's
  91.  */
  92.  
  93. /* Chain containing all defined commands.  */
  94.  
  95. struct cmd_list_element *cmdlist;
  96.  
  97. /* Chain containing all defined info subcommands.  */
  98.  
  99. struct cmd_list_element *infolist;
  100.  
  101. /* Chain containing all defined enable subcommands. */
  102.  
  103. struct cmd_list_element *enablelist;
  104.  
  105. /* Chain containing all defined disable subcommands. */
  106.  
  107. struct cmd_list_element *disablelist;
  108.  
  109. /* Chain containing all defined delete subcommands. */
  110.  
  111. struct cmd_list_element *deletelist;
  112.  
  113. /* Chain containing all defined "enable breakpoint" subcommands. */
  114.  
  115. struct cmd_list_element *enablebreaklist;
  116.  
  117. /* Chain containing all defined set subcommands */
  118.  
  119. struct cmd_list_element *setlist;
  120.  
  121. /* Chain containing all defined \"set history\".  */
  122.  
  123. struct cmd_list_element *sethistlist;
  124.  
  125. /* Chain containing all defined \"unset history\".  */
  126.  
  127. struct cmd_list_element *unsethistlist;
  128.  
  129. /* stdio stream that command input is being read from.  */
  130.  
  131. FILE *instream;
  132.  
  133. /* Current working directory.  */
  134.  
  135. char *current_directory;
  136.  
  137. /* The directory name is actually stored here (usually).  */
  138. static char dirbuf[MAXPATHLEN];
  139.  
  140. /* The number of lines on a page, and the number of spaces
  141.    in a line.  */
  142. int linesize, pagesize;
  143.  
  144. /* Nonzero if we should refrain from using an X window.  */
  145.  
  146. int inhibit_windows = 0;
  147.  
  148. /* Function to call before reading a command, if nonzero.
  149.    The function receives two args: an input stream,
  150.    and a prompt string.  */
  151.    
  152. void (*window_hook) ();
  153.  
  154. extern int frame_file_full_name;
  155. int xgdb_verbose;
  156.  
  157. void free_command_lines ();
  158. char *gdb_readline ();
  159. char *command_line_input ();
  160. static void initialize_main ();
  161. static void initialize_cmd_lists ();
  162. void command_loop ();
  163. static void source_command ();
  164. static void print_gdb_version ();
  165. static void float_handler ();
  166. static void cd_command ();
  167.  
  168. char *getenv ();
  169.  
  170. /* gdb prints this when reading a command interactively */
  171. static char *prompt;
  172.  
  173. /* Buffer used for reading command lines, and the size
  174.    allocated for it so far.  */
  175.  
  176. char *line;
  177. int linesize;
  178.  
  179.  
  180. /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT.  */
  181.  
  182. #ifndef STOP_SIGNAL
  183. #ifdef SIGTSTP
  184. #define STOP_SIGNAL SIGTSTP
  185. #endif
  186. #endif
  187.  
  188. /* This is how `error' returns to command level.  */
  189.  
  190. jmp_buf to_top_level;
  191.  
  192. void
  193. return_to_top_level ()
  194. {
  195.   quit_flag = 0;
  196.   immediate_quit = 0;
  197.   clear_breakpoint_commands ();
  198.   clear_momentary_breakpoints ();
  199.   disable_current_display ();
  200.   do_cleanups (0);
  201.   longjmp (to_top_level, 1);
  202. }
  203.  
  204. /* Call FUNC with arg ARG, catching any errors.
  205.    If there is no error, return the value returned by FUNC.
  206.    If there is an error, return zero after printing ERRSTRING
  207.     (which is in addition to the specific error message already printed).  */
  208.  
  209. int
  210. catch_errors (func, arg, errstring)
  211.      int (*func) ();
  212.      int arg;
  213.      char *errstring;
  214. {
  215.   jmp_buf saved;
  216.   int val;
  217.   struct cleanup *saved_cleanup_chain;
  218.  
  219.   saved_cleanup_chain = save_cleanups ();
  220.  
  221.   bcopy (to_top_level, saved, sizeof (jmp_buf));
  222.  
  223.   if (setjmp (to_top_level) == 0)
  224.     val = (*func) (arg);
  225.   else
  226.     {
  227.       fprintf (stderr, "%s\n", errstring);
  228.       val = 0;
  229.     }
  230.  
  231.   restore_cleanups (saved_cleanup_chain);
  232.  
  233.   bcopy (saved, to_top_level, sizeof (jmp_buf));
  234.   return val;
  235. }
  236.  
  237. /* Handler for SIGHUP.  */
  238.  
  239. static void
  240. disconnect ()
  241. {
  242.   kill_inferior_fast ();
  243.   signal (SIGHUP, SIG_DFL);
  244.   kill (getpid (), SIGHUP);
  245. }
  246.  
  247. /* Clean up on error during a "source" command (or execution of a
  248.    user-defined command).
  249.    Close the file opened by the command
  250.    and restore the previous input stream.  */
  251.  
  252. static void
  253. source_cleanup (stream)
  254.      FILE *stream;
  255. {
  256.   /* Instream may be 0; set to it when executing user-defined command. */
  257.   if (instream)
  258.     fclose (instream);
  259.   instream = stream;
  260. }
  261.  
  262.  
  263. int
  264. main (argc, argv, envp)
  265.      int argc;
  266.      char **argv;
  267.      char **envp;
  268. {
  269.   int count;
  270.   int inhibit_gdbinit = 0;
  271.   int quiet = 0;
  272.   int batch = 0;
  273.   register int i;
  274.  
  275. #if defined (ALIGN_STACK_ON_STARTUP)
  276.   i = (int) &count & 0x3;
  277.   if (i != 0)
  278.     alloca (4 - i);
  279. #endif
  280.  
  281.   quit_flag = 0;
  282.   linesize = 100;
  283.   line = (char *) xmalloc (linesize);
  284.   instream = stdin;
  285.  
  286.   getwd (dirbuf);
  287.   current_directory = dirbuf;
  288.  
  289. #ifdef SET_STACK_LIMIT_HUGE
  290.   {
  291.     struct rlimit rlim;
  292.  
  293.     /* Set the stack limit huge so that alloca (particularly stringtab
  294.      * in dbxread.c) does not fail. */
  295.     getrlimit (RLIMIT_STACK, &rlim);
  296.     original_stack_limit = rlim.rlim_cur;
  297.     rlim.rlim_cur = rlim.rlim_max;
  298.     setrlimit (RLIMIT_STACK, &rlim);
  299.   }
  300. #endif /* SET_STACK_LIMIT_HUGE */
  301.  
  302.   /* Look for flag arguments.  */
  303.  
  304.   for (i = 1; i < argc; i++)
  305.     {
  306.       if (!strcmp (argv[i], "-q") || !strcmp (argv[i], "-quiet"))
  307.     quiet = 1;
  308.       else if (!strcmp (argv[i], "-nx"))
  309.     inhibit_gdbinit = 1;
  310.       else if (!strcmp (argv[i], "-nw"))
  311.     inhibit_windows = 1;
  312.       else if (!strcmp (argv[i], "-batch"))
  313.     batch = 1, quiet = 1;
  314.       else if (!strcmp (argv[i], "-fullname"))
  315.     frame_file_full_name = 1;
  316.       else if (!strcmp (argv[i], "-xgdb_verbose"))
  317.     xgdb_verbose = 1;
  318.       /* -help: print a summary of command line switches.  */
  319.       else if (!strcmp (argv[i], "-help"))
  320.     {
  321.       fputs ("\
  322. This is GDB, the GNU debugger.  Use the command\n\
  323.     gdb [options] [executable [core-file]]\n\
  324. to enter the debugger.\n\
  325. \n\
  326. Options available are:\n\
  327.   -help             Print this message.\n\
  328.   -quiet            Do not print version number on startup.\n\
  329.   -fullname         Output information used by emacs-GDB interface.\n\
  330.   -batch            Exit after processing options.\n\
  331.   -nx               Do not read .gdbinit file.\n\
  332.   -tty TTY          Use TTY for input/output by the program being debugged.\n\
  333.   -cd DIR           Change current directory to DIR.\n\
  334.   -directory DIR    Search for source files in DIR.\n\
  335.   -command FILE     Execute GDB commands from FILE.\n\
  336.   -symbols SYMFILE  Read symbols from SYMFILE.\n\
  337.   -exec EXECFILE    Use EXECFILE as the executable.\n\
  338.   -se FILE          Use FILE as symbol file and executable file.\n\
  339.   -core COREFILE    Analyze the core dump COREFILE.\n\
  340. \n\
  341. For more information, type \"help\" from within GDB, or consult the\n\
  342. GDB manual (available as on-line info or a printed manual).\n", stderr);
  343.       /* Exiting after printing this message seems like
  344.          the most useful thing to do.  */
  345.       exit (0);
  346.     }
  347.       else if (argv[i][0] == '-')
  348.     /* Other options take arguments, so don't confuse an
  349.        argument with an option.  */
  350.     i++;
  351.     }
  352.  
  353.   /* Run the init function of each source file */
  354.  
  355.   initialize_cmd_lists ();    /* This needs to be done first */
  356.   initialize_all_files ();
  357.   initialize_main ();        /* But that omits this file!  Do it now */
  358.   initialize_signals ();
  359.  
  360.   if (!quiet)
  361.     print_gdb_version ();
  362.  
  363.   /* Process the command line arguments.  */
  364.  
  365.   count = 0;
  366.   for (i = 1; i < argc; i++)
  367.     {
  368.       register char *arg = argv[i];
  369.       /* Args starting with - say what to do with the following arg
  370.      as a filename.  */
  371.       if (arg[0] == '-')
  372.     {
  373.       extern void exec_file_command (), symbol_file_command ();
  374.       extern void core_file_command (), directory_command ();
  375.       extern void tty_command ();
  376.  
  377.       if (!strcmp (arg, "-q") || !strcmp (arg, "-nx")
  378.           || !strcmp (arg, "-quiet") || !strcmp (arg, "-batch")
  379.           || !strcmp (arg, "-fullname") || !strcmp (arg, "-nw")
  380.           || !strcmp (arg, "-xgdb_verbose")
  381.           || !strcmp (arg, "-help"))
  382.         /* Already processed above */
  383.         continue;
  384.  
  385.       if (++i == argc)
  386.         fprintf (stderr, "No argument follows \"%s\".\n", arg);
  387.       if (!setjmp (to_top_level))
  388.         {
  389.           /* -s foo: get syms from foo.  -e foo: execute foo.
  390.          -se foo: do both with foo.  -c foo: use foo as core dump.  */
  391.           if (!strcmp (arg, "-se"))
  392.         {
  393.           exec_file_command (argv[i], !batch);
  394.           symbol_file_command (argv[i], !batch);
  395.         }
  396.           else if (!strcmp (arg, "-s") || !strcmp (arg, "-symbols"))
  397.         symbol_file_command (argv[i], !batch);
  398.           else if (!strcmp (arg, "-e") || !strcmp (arg, "-exec"))
  399.         exec_file_command (argv[i], !batch);
  400.           else if (!strcmp (arg, "-c") || !strcmp (arg, "-core"))
  401. #if (defined(sprite) && defined(mips))
  402.         error("\"%s\" option not supported on mips yet\n", arg);
  403. #else
  404.         core_file_command (argv[i], !batch);
  405. #endif
  406.           /* -x foo: execute commands from foo.  */
  407.           else if (!strcmp (arg, "-x") || !strcmp (arg, "-command")
  408.                || !strcmp (arg, "-commands"))
  409.         source_command (argv[i]);
  410.           /* -d foo: add directory `foo' to source-file directory
  411.                  search-list */
  412.           else if (!strcmp (arg, "-d") || !strcmp (arg, "-dir")
  413.                || !strcmp (arg, "-directory"))
  414.         directory_command (argv[i], 0);
  415.           /* -cd FOO: specify current directory as FOO.
  416.          GDB remembers the precise string FOO as the dirname.  */
  417.           else if (!strcmp (arg, "-cd"))
  418.         {
  419.           cd_command (argv[i], 0);
  420.           init_source_path ();
  421.         }
  422.           /* -t /def/ttyp1: use /dev/ttyp1 for inferior I/O.  */
  423.           else if (!strcmp (arg, "-t") || !strcmp (arg, "-tty"))
  424.         tty_command (argv[i], 0);
  425.  
  426.           else
  427.         error ("Unknown command-line switch: \"%s\"\n", arg);
  428.         }
  429.     }
  430.       else
  431.     {
  432.       /* Args not thus accounted for
  433.          are treated as, first, the symbol/executable file
  434.          and, second, the core dump file.  */
  435.       count++;
  436.       if (!setjmp (to_top_level))
  437.         switch (count)
  438.           {
  439.           case 1:
  440.         exec_file_command (arg, !batch);
  441.         symbol_file_command (arg, !batch);
  442.         break;
  443.  
  444.           case 2:
  445. #if (defined(sprite) && defined(mips))
  446.         error("Core files not supported on mips yet\n");
  447. #else
  448.         core_file_command (arg, !batch);
  449. #endif
  450.         break;
  451.  
  452.           case 3:
  453.         fprintf (stderr, "Excess command line args ignored. (%s%s)\n",
  454.              arg, (i == argc - 1) ? "" : " ...");
  455.           }
  456.     }
  457.     }
  458.  
  459.   {
  460.     struct stat homebuf, cwdbuf;
  461.     char *homedir, *homeinit;
  462.  
  463.     /* Read init file, if it exists in home directory  */
  464.     homedir = getenv ("HOME");
  465.     if (homedir)
  466.       {
  467.     homeinit = (char *) alloca (strlen (getenv ("HOME")) + 10);
  468.     strcpy (homeinit, getenv ("HOME"));
  469.     strcat (homeinit, "/.gdbinit");
  470.     if (!inhibit_gdbinit && access (homeinit, R_OK) == 0)
  471.       if (!setjmp (to_top_level))
  472.         source_command (homeinit);
  473.  
  474.     /* Do stats; no need to do them elsewhere since we'll only
  475.        need them if homedir is set.  Make sure that they are
  476.        zero in case one of them fails (guarantees that they
  477.        won't match if either exits).  */
  478.     
  479.     bzero (&homebuf, sizeof (struct stat));
  480.     bzero (&cwdbuf, sizeof (struct stat));
  481.     
  482.     stat (homeinit, &homebuf);
  483.     stat ("./.gdbinit", &cwdbuf); /* We'll only need this if
  484.                      homedir was set.  */
  485.       }
  486.     
  487.     /* Read the input file in the current directory, *if* it isn't
  488.        the same file (it should exist, also).  */
  489.  
  490.     if (!homedir
  491.     || bcmp ((char *) &homebuf,
  492.          (char *) &cwdbuf,
  493.          sizeof (struct stat)))
  494.       if (!inhibit_gdbinit && access (".gdbinit", R_OK) == 0)
  495.     if (!setjmp (to_top_level))
  496.       source_command (".gdbinit");
  497.   }
  498.  
  499.   if (batch)
  500.     {
  501. #if 0
  502.       fatal ("Attempt to read commands from stdin in batch mode.");
  503. #endif
  504.       /* We have hit the end of the batch file.  */
  505.       exit (0);
  506.     }
  507.  
  508.   if (!quiet)
  509.     printf ("Type \"help\" for a list of commands.\n");
  510.  
  511.   /* The command loop.  */
  512.  
  513.   while (1)
  514.     {
  515.       if (!setjmp (to_top_level))
  516.     command_loop ();
  517.       clearerr (stdin);        /* Don't get hung if C-d is typed.  */
  518.     }
  519. }
  520.  
  521. /* Execute the line P as a command.
  522.    Pass FROM_TTY as second argument to the defining function.  */
  523.  
  524. void
  525. execute_command (p, from_tty)
  526.      char *p;
  527.      int from_tty;
  528. {
  529.   register struct cmd_list_element *c;
  530.   register struct command_line *cmdlines;
  531.  
  532.   free_all_values ();
  533.   while (*p == ' ' || *p == '\t') p++;
  534.   if (*p)
  535.     {
  536.       c = lookup_cmd (&p, cmdlist, "", 0, 1);
  537.       if (c->function == 0)
  538.     error ("That is not a command, just a help topic.");
  539.       else if (c->class == (int) class_user)
  540.     {
  541.       struct cleanup *old_chain;
  542.       
  543.       if (*p)
  544.         error ("User-defined commands cannot take arguments.");
  545.       cmdlines = (struct command_line *) c->function;
  546.       if (cmdlines == (struct command_line *) 0)
  547.         /* Null command */
  548.         return;
  549.  
  550.       /* Set the instream to 0, indicating execution of a
  551.          user-defined function.  */
  552.       old_chain =  make_cleanup (source_cleanup, instream);
  553.       instream = (FILE *) 0;
  554.       while (cmdlines)
  555.         {
  556.           execute_command (cmdlines->line, 0);
  557.           cmdlines = cmdlines->next;
  558.         }
  559.       do_cleanups (old_chain);
  560.     }
  561.       else
  562.     /* Pass null arg rather than an empty one.  */
  563.     (*c->function) (*p ? p : 0, from_tty);
  564.     }
  565. }
  566.  
  567. static void
  568. do_nothing ()
  569. {
  570. }
  571.  
  572. /* Read commands from `instream' and execute them
  573.    until end of file.  */
  574. void
  575. command_loop ()
  576. {
  577.   struct cleanup *old_chain;
  578.   while (!feof (instream))
  579.     {
  580.       if (window_hook && instream == stdin)
  581.     (*window_hook) (instream, prompt);
  582.  
  583.       quit_flag = 0;
  584.       if (instream == stdin && ISATTY (stdin))
  585.     reinitialize_more_filter ();
  586.       old_chain = make_cleanup (do_nothing, 0);
  587.       execute_command (command_line_input (instream == stdin ? prompt : 0,
  588.                       instream == stdin),
  589.                instream == stdin);
  590.       /* Do any commands attached to breakpoint we stopped at.  */
  591.       do_breakpoint_commands ();
  592.       do_cleanups (old_chain);
  593.     }
  594. }
  595.  
  596. /* Commands call this if they do not want to be repeated by null lines.  */
  597.  
  598. void
  599. dont_repeat ()
  600. {
  601.   /* If we aren't reading from standard input, we are saving the last
  602.      thing read from stdin in line and don't want to delete it.  Null lines
  603.      won't repeat here in any case.  */
  604.   if (instream == stdin)
  605.     *line = 0;
  606. }
  607.  
  608. /* Read a line from the stream "instream" without command line editing.
  609.  
  610.    It prints PROMPT once at the start.
  611.  
  612.    If RETURN_RESULT is set it allocates
  613.    space for whatever the user types and returns the result.
  614.    If not, it just discards what the user types.  */
  615. char *
  616. gdb_readline (prompt, return_result)
  617.      char *prompt;
  618.      int return_result;
  619. {
  620.   int c;
  621.   char *result;
  622.   int input_index = 0;
  623.   int result_size = 80;
  624.  
  625.   if (prompt)
  626.     {
  627.       printf (prompt);
  628.       fflush (stdout);
  629.     }
  630.   
  631.   if (return_result)
  632.     result = (char *) xmalloc (result_size);
  633.  
  634.   while (1)
  635.     {
  636.       c = fgetc (instream);
  637.       if (c == -1 || c == '\n')
  638.     break;
  639.       if (return_result)
  640.     {
  641.       result[input_index++] = c;
  642.       while (input_index >= result_size)
  643.         {
  644.           result_size *= 2;
  645.           result = (char *) xrealloc (result, result_size);
  646.         }
  647.     }
  648.     }
  649.   if (return_result)
  650.     {
  651.       result[input_index++] = '\0';
  652.       return result;
  653.     }
  654.   else
  655.     return (char *) 0;
  656. }
  657.  
  658. /* Declaration for fancy readline with command line editing.  */
  659. char *readline ();
  660.  
  661. /* Variables which control command line editing and history
  662.    substitution.  These variables are given default values at the end
  663.    of this file.  */
  664. static int command_editing_p;
  665. static int history_expansion_p;
  666. static int write_history_p;
  667. static int history_size;
  668. static char *history_filename;
  669.  
  670. /* Variables which are necessary for fancy command line editing.  */
  671. char *gdb_completer_word_break_characters =
  672.   " \t\n!@@#$%^&*()-+=|~`}{[]\"';:?/>.<,";
  673.  
  674. /* Functions that are used as part of the fancy command line editing.  */
  675.  
  676. /* Generate symbol names one by one for the completer.  If STATE is
  677.    zero, then we need to initialize, otherwise the initialization has
  678.    already taken place.  TEXT is what we expect the symbol to start
  679.    with.  RL_LINE_BUFFER is available to be looked at; it contains the
  680.    entire text of the line.  RL_POINT is the offset in that line of
  681.    the cursor.  You should pretend that the line ends at RL_POINT.  */
  682. char *
  683. symbol_completion_function (text, state)
  684.      char *text;
  685.      int state;
  686. {
  687.   char **make_symbol_completion_list ();
  688.   static char **list = (char **)NULL;
  689.   static int index;
  690.   char *output;
  691.   extern char *rl_line_buffer;
  692.   extern int rl_point;
  693.   char *tmp_command, *p;
  694.   struct cmd_list_element *c, *result_list;
  695.  
  696.   if (!state)
  697.     {
  698.       /* Free the storage used by LIST, but not by the strings inside.  This is
  699.      because rl_complete_internal () frees the strings. */
  700.       if (list)
  701.     free (list);
  702.       list = 0;
  703.       index = 0;
  704.  
  705.       /* Decide whether to complete on a list of gdb commands or on
  706.      symbols.  */
  707.       tmp_command = (char *) alloca (rl_point + 1);
  708.       p = tmp_command;
  709.       
  710.       strncpy (tmp_command, rl_line_buffer, rl_point);
  711.       tmp_command[rl_point] = '\0';
  712.  
  713.       if (rl_point == 0)
  714.     {
  715.       /* An empty line we want to consider ambiguous; that is,
  716.          it could be any command.  */
  717.       c = (struct cmd_list_element *) -1;
  718.       result_list = 0;
  719.     }
  720.       else
  721.     c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
  722.  
  723.       /* Move p up to the next interesting thing.  */
  724.       while (*p == ' ' || *p == '\t')
  725.     p++;
  726.  
  727.       if (!c)
  728.     /* He's typed something unrecognizable.  Sigh.  */
  729.     list = (char **) 0;
  730.       else if (c == (struct cmd_list_element *) -1)
  731.     {
  732.       if (p + strlen(text) != tmp_command + rl_point)
  733.         error ("Unrecognized command.");
  734.       
  735.       /* He's typed something ambiguous.  This is easier.  */
  736.       if (result_list)
  737.         list = complete_on_cmdlist (*result_list->prefixlist, text);
  738.       else
  739.         list = complete_on_cmdlist (cmdlist, text);
  740.     }
  741.       else
  742.     {
  743.       /* If we've gotten this far, gdb has recognized a full
  744.          command.  There are several possibilities:
  745.  
  746.          1) We need to complete on the command.
  747.          2) We need to complete on the possibilities coming after
  748.          the command.
  749.          2) We need to complete the text of what comes after the
  750.          command.   */
  751.  
  752.       if (!*p && *text)
  753.         /* Always (might be longer versions of thie command).  */
  754.         list = complete_on_cmdlist (result_list, text);
  755.       else if (!*p && !*text)
  756.         {
  757.           if (c->prefixlist)
  758.         list = complete_on_cmdlist (*c->prefixlist, "");
  759.           else
  760.         list = make_symbol_completion_list ("");
  761.         }
  762.       else
  763.         {
  764.           if (c->prefixlist && !c->allow_unknown)
  765.         {
  766.           *p = '\0';
  767.           error ("\"%s\" command requires a subcommand.",
  768.              tmp_command);
  769.         }
  770.           else
  771.         list = make_symbol_completion_list (text);
  772.         }
  773.     }
  774.     }
  775.  
  776.   /* If the debugged program wasn't compiled with symbols, or if we're
  777.      clearly completing on a command and no command matches, return
  778.      NULL.  */
  779.   if (!list)
  780.     return ((char *)NULL);
  781.  
  782.   output = list[index];
  783.   if (output)
  784.     index++;
  785.  
  786.   return (output);
  787. }
  788.  
  789. #ifdef STOP_SIGNAL
  790. static void
  791. stop_sig ()
  792. {
  793. #if STOP_SIGNAL == SIGTSTP
  794.   signal (SIGTSTP, SIG_DFL);
  795.   sigsetmask (0);
  796.   kill (getpid (), SIGTSTP);
  797.   signal (SIGTSTP, stop_sig);
  798. #else
  799.   signal (STOP_SIGNAL, stop_sig);
  800. #endif
  801.   printf ("%s", prompt);
  802.   fflush (stdout);
  803.  
  804.   /* Forget about any previous command -- null line now will do nothing.  */
  805.   dont_repeat ();
  806. }
  807. #endif /* STOP_SIGNAL */
  808.  
  809. #if 0
  810. Writing the history file upon a terminating signal is not useful,
  811.   because the info is rarely relevant and is in the core dump anyway.
  812.   It is an annoyance to have the file cluttering up the place.
  813. /* The list of signals that would terminate us if not caught.
  814.    We catch them, but just so that we can write the history file,
  815.    and so forth. */
  816. int terminating_signals[] = {
  817.   SIGHUP, SIGINT, SIGILL, SIGTRAP, SIGIOT,
  818.   SIGEMT, SIGFPE, SIGKILL, SIGBUS, SIGSEGV, SIGSYS,
  819.   SIGPIPE, SIGALRM, SIGTERM,
  820. #ifdef SIGXCPU
  821.   SIGXCPU,
  822. #endif
  823. #ifdef SIGXFSZ
  824.   SIGXFSZ,
  825. #endif
  826. #ifdef SIGVTALRM
  827.   SIGVTALRM,
  828. #endif
  829. #ifdef SIGPROF
  830.   SIGPROF,
  831. #endif
  832. #ifdef SIGLOST
  833.   SIGLOST,
  834. #endif
  835. #ifdef SIGUSR1
  836.   SIGUSR1, SIGUSR2
  837. #endif
  838.     };
  839.  
  840. #define TERMSIGS_LENGTH (sizeof (terminating_signals) / sizeof (int))
  841.  
  842. static void
  843. catch_termination (sig)
  844.      int sig;
  845. {
  846.   /* We are probably here because GDB has a bug.  Write out the history
  847.      so that we might have a better chance of reproducing it.  */
  848.   /* Tell the user what we are doing so he can delete the file if
  849.      it is unwanted.  */
  850.   write_history (history_filename);
  851.   printf ("\n%s written.\n", history_filename);
  852.   signal (sig, SIG_DFL);
  853.   kill (getpid (), sig);
  854. }
  855. #endif
  856.  
  857. /* Initialize signal handlers. */
  858. initialize_signals ()
  859. {
  860.   extern void request_quit ();
  861. #if 0
  862.   register int i;
  863.  
  864.   for (i = 0; i < TERMSIGS_LENGTH; i++)
  865.     signal (terminating_signals[i], catch_termination);
  866. #endif
  867.  
  868.   signal (SIGINT, request_quit);
  869.  
  870.   /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
  871.      passed to the inferior, which we don't want.  It would be
  872.      possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
  873.      on BSD4.3 systems using vfork, that will (apparently) affect the
  874.      GDB process as well as the inferior (the signal handling tables
  875.      being shared between the two, apparently).  Since we establish
  876.      a handler for SIGQUIT, when we call exec it will set the signal
  877.      to SIG_DFL for us.  */
  878.   signal (SIGQUIT, do_nothing);
  879.   if (signal (SIGHUP, do_nothing) != SIG_IGN)
  880.     signal (SIGHUP, disconnect);
  881.   signal (SIGFPE, float_handler);
  882. }
  883.  
  884. /* Read one line from the command input stream `instream'
  885.    into the local static buffer `linebuffer' (whose current length
  886.    is `linelength').
  887.    The buffer is made bigger as necessary.
  888.    Returns the address of the start of the line.
  889.  
  890.    *If* the instream == stdin & stdin is a terminal, the line read
  891.    is copied into the file line saver (global var char *line,
  892.    length linesize) so that it can be duplicated.
  893.  
  894.    This routine either uses fancy command line editing or
  895.    simple input as the user has requested.  */
  896.  
  897. char *
  898. command_line_input (prompt, repeat)
  899.      char *prompt;
  900.      int repeat;
  901. {
  902.   static char *linebuffer = 0;
  903.   static int linelength = 0;
  904.   register char *p;
  905.   register char *p1, *rl;
  906.   char *local_prompt = prompt;
  907.   register int c;
  908.   char *nline;
  909.  
  910.   if (linebuffer == 0)
  911.     {
  912.       linelength = 80;
  913.       linebuffer = (char *) xmalloc (linelength);
  914.     }
  915.  
  916.   p = linebuffer;
  917.  
  918.   /* Control-C quits instantly if typed while in this loop
  919.      since it should not wait until the user types a newline.  */
  920.   immediate_quit++;
  921. #ifdef STOP_SIGNAL
  922.   signal (STOP_SIGNAL, stop_sig);
  923. #endif
  924.  
  925.   while (1)
  926.     {
  927.       /* Don't use fancy stuff if not talking to stdin.  */
  928.       if (command_editing_p && instream == stdin
  929.       && ISATTY (instream))
  930.     rl = readline (local_prompt);
  931.       else
  932.     rl = gdb_readline (local_prompt, 1);
  933.  
  934.       if (!rl || rl == (char *) EOF) break;
  935.       if (strlen(rl) + 1 + (p - linebuffer) > linelength)
  936.     {
  937.       linelength = strlen(rl) + 1 + (p - linebuffer);
  938.       nline = (char *) xrealloc (linebuffer, linelength);
  939.       p += nline - linebuffer;
  940.       linebuffer = nline;
  941.     }
  942.       p1 = rl;
  943.       /* Copy line.  Don't copy null at end.  (Leaves line alone
  944.          if this was just a newline)  */
  945.       while (*p1)
  946.     *p++ = *p1++;
  947.  
  948.       free (rl);            /* Allocated in readline.  */
  949.  
  950.       if (p == linebuffer || *(p - 1) != '\\')
  951.     break;
  952.  
  953.       p--;            /* Put on top of '\'.  */
  954.       local_prompt = (char *) 0;
  955.   }
  956.  
  957. #ifdef STOP_SIGNAL
  958.   signal (SIGTSTP, SIG_DFL);
  959. #endif
  960.   immediate_quit--;
  961.  
  962.   /* Do history expansion if that is wished.  */
  963.   if (history_expansion_p && instream == stdin
  964.       && ISATTY (instream))
  965.     {
  966.       char *history_value;
  967.       int expanded;
  968.  
  969.       *p = '\0';        /* Insert null now.  */
  970.       expanded = history_expand (linebuffer, &history_value);
  971.       if (expanded)
  972.     {
  973.       /* Print the changes.  */
  974.       printf ("%s\n", history_value);
  975.  
  976.       /* If there was an error, call this function again.  */
  977.       if (expanded < 0)
  978.         {
  979.           free (history_value);
  980.           return command_line_input (prompt, repeat);
  981.         }
  982.       if (strlen (history_value) > linelength)
  983.         {
  984.           linelength = strlen (history_value) + 1;
  985.           linebuffer = (char *) xrealloc (linebuffer, linelength);
  986.         }
  987.       strcpy (linebuffer, history_value);
  988.       p = linebuffer + strlen(linebuffer);
  989.       free (history_value);
  990.     }
  991.     }
  992.  
  993.   /* If we just got an empty line, and that is supposed
  994.      to repeat the previous command, return the value in the
  995.      global buffer.  */
  996.   if (repeat)
  997.     {
  998.       if (p == linebuffer)
  999.     return line;
  1000.       p1 = linebuffer;
  1001.       while (*p1 == ' ' || *p1 == '\t')
  1002.     p1++;
  1003.       if (!*p1)
  1004.     return line;
  1005.     }
  1006.  
  1007.   *p = 0;
  1008.  
  1009.   /* Add line to history if appropriate.  */
  1010.   if (instream == stdin
  1011.       && ISATTY (stdin) && *linebuffer)
  1012.     add_history (linebuffer);
  1013.  
  1014.   /* If line is a comment, clear it out.  */
  1015.   /* Note:  comments are added to the command history.
  1016.      This is useful when you type a command, and then realize
  1017.      you don't want to execute it quite yet.  You can comment out the
  1018.      command and then later fetch it from the value history and
  1019.      remove the '#'.  */
  1020.   p1 = linebuffer;
  1021.   while ((c = *p1) == ' ' || c == '\t') p1++;
  1022.   if (c == '#')
  1023.     *linebuffer = 0;
  1024.  
  1025.   /* Save into global buffer if appropriate.  */
  1026.   if (repeat)
  1027.     {
  1028.       if (linelength > linesize)
  1029.     {
  1030.       line = xrealloc (line, linelength);
  1031.       linesize = linelength;
  1032.     }
  1033.       strcpy (line, linebuffer);
  1034.       return line;
  1035.     }
  1036.  
  1037.   return linebuffer;
  1038. }
  1039.  
  1040. /* Read lines from the input stream
  1041.    and accumulate them in a chain of struct command_line's
  1042.    which is then returned.  */
  1043.  
  1044. struct command_line *
  1045. read_command_lines ()
  1046. {
  1047.   struct command_line *first = 0;
  1048.   register struct command_line *next, *tail = 0;
  1049.   register char *p, *p1;
  1050.   struct cleanup *old_chain = 0;
  1051.  
  1052.   while (1)
  1053.     {
  1054.       dont_repeat ();
  1055.       p = command_line_input (0, instream == stdin);
  1056.       /* Remove leading and trailing blanks.  */
  1057.       while (*p == ' ' || *p == '\t') p++;
  1058.       p1 = p + strlen (p);
  1059.       while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
  1060.  
  1061.       /* Is this "end"?  */
  1062.       if (p1 - p == 3 && !strncmp (p, "end", 3))
  1063.     break;
  1064.  
  1065.       /* No => add this line to the chain of command lines.  */
  1066.       next = (struct command_line *) xmalloc (sizeof (struct command_line));
  1067.       next->line = savestring (p, p1 - p);
  1068.       next->next = 0;
  1069.       if (tail)
  1070.     {
  1071.       tail->next = next;
  1072.     }
  1073.       else
  1074.     {
  1075.       /* We just read the first line.
  1076.          From now on, arrange to throw away the lines we have
  1077.          if we quit or get an error while inside this function.  */
  1078.       first = next;
  1079.       old_chain = make_cleanup (free_command_lines, &first);
  1080.     }
  1081.       tail = next;
  1082.     }
  1083.  
  1084.   dont_repeat ();
  1085.  
  1086.   /* Now we are about to return the chain to our caller,
  1087.      so freeing it becomes his responsibility.  */
  1088.   if (first)
  1089.     discard_cleanups (old_chain);
  1090.   return first;
  1091. }
  1092.  
  1093. /* Free a chain of struct command_line's.  */
  1094.  
  1095. void
  1096. free_command_lines (lptr)
  1097.       struct command_line **lptr;
  1098. {
  1099.   register struct command_line *l = *lptr;
  1100.   register struct command_line *next;
  1101.  
  1102.   while (l)
  1103.     {
  1104.       next = l->next;
  1105.       free (l->line);
  1106.       free (l);
  1107.       l = next;
  1108.     }
  1109. }
  1110.  
  1111. /* Add an element to the list of info subcommands.  */
  1112.  
  1113. void
  1114. add_info (name, fun, doc)
  1115.      char *name;
  1116.      void (*fun) ();
  1117.      char *doc;
  1118. {
  1119.   add_cmd (name, no_class, fun, doc, &infolist);
  1120. }
  1121.  
  1122. /* Add an alias to the list of info subcommands.  */
  1123.  
  1124. void
  1125. add_info_alias (name, oldname, abbrev_flag)
  1126.      char *name;
  1127.      char *oldname;
  1128.      int abbrev_flag;
  1129. {
  1130.   add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
  1131. }
  1132.  
  1133. /* The "info" command is defined as a prefix, with allow_unknown = 0.
  1134.    Therefore, its own definition is called only for "info" with no args.  */
  1135.  
  1136. static void
  1137. info_command ()
  1138. {
  1139.   printf ("\"info\" must be followed by the name of an info command.\n");
  1140.   help_list (infolist, "info ", -1, stdout);
  1141. }
  1142.  
  1143. /* Add an element to the list of commands.  */
  1144.  
  1145. void
  1146. add_com (name, class, fun, doc)
  1147.      char *name;
  1148.      int class;
  1149.      void (*fun) ();
  1150.      char *doc;
  1151. {
  1152.   add_cmd (name, class, fun, doc, &cmdlist);
  1153. }
  1154.  
  1155. /* Add an alias or abbreviation command to the list of commands.  */
  1156.  
  1157. void
  1158. add_com_alias (name, oldname, class, abbrev_flag)
  1159.      char *name;
  1160.      char *oldname;
  1161.      int class;
  1162.      int abbrev_flag;
  1163. {
  1164.   add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
  1165. }
  1166.  
  1167. void
  1168. error_no_arg (why)
  1169.      char *why;
  1170. {
  1171.   error ("Argument required (%s).", why);
  1172. }
  1173.  
  1174. static void
  1175. help_command (command, from_tty)
  1176.      char *command;
  1177.      int from_tty; /* Ignored */
  1178. {
  1179.   help_cmd (command, stdout);
  1180. }
  1181.  
  1182. static void
  1183. validate_comname (comname)
  1184.      char *comname;
  1185. {
  1186.   register char *p;
  1187.  
  1188.   if (comname == 0)
  1189.     error_no_arg ("name of command to define");
  1190.  
  1191.   p = comname;
  1192.   while (*p)
  1193.     {
  1194.       if (!(*p >= 'A' && *p <= 'Z')
  1195.       && !(*p >= 'a' && *p <= 'z')
  1196.       && !(*p >= '0' && *p <= '9')
  1197.       && *p != '-')
  1198.     error ("Junk in argument list: \"%s\"", p);
  1199.       p++;
  1200.     }
  1201. }
  1202.  
  1203. static void
  1204. define_command (comname, from_tty)
  1205.      char *comname;
  1206.      int from_tty;
  1207. {
  1208.   register struct command_line *cmds;
  1209.   register struct cmd_list_element *c;
  1210.   char *tem = comname;
  1211.  
  1212.   validate_comname (comname);
  1213.  
  1214.   c = lookup_cmd (&tem, cmdlist, "", -1, 1);
  1215.   if (c)
  1216.     {
  1217.       if (c->class == (int) class_user || c->class == (int) class_alias)
  1218.     tem = "Redefine command \"%s\"? ";
  1219.       else
  1220.     tem = "Really redefine built-in command \"%s\"? ";
  1221.       if (!query (tem, comname))
  1222.     error ("Command \"%s\" not redefined.", comname);
  1223.     }
  1224.  
  1225.   if (from_tty)
  1226.     {
  1227.       printf ("Type commands for definition of \"%s\".\n\
  1228. End with a line saying just \"end\".\n", comname);
  1229.       fflush (stdout);
  1230.     }
  1231.   comname = savestring (comname, strlen (comname));
  1232.  
  1233.   cmds = read_command_lines ();
  1234.  
  1235.   if (c && c->class == (int) class_user)
  1236.     free_command_lines (&c->function);
  1237.  
  1238.   add_com (comname, class_user, cmds,
  1239.        (c && c->class == (int) class_user)
  1240.        ? c->doc : savestring ("User-defined.", 13));
  1241. }
  1242.  
  1243. static void
  1244. document_command (comname, from_tty)
  1245.      char *comname;
  1246.      int from_tty;
  1247. {
  1248.   struct command_line *doclines;
  1249.   register struct cmd_list_element *c;
  1250.   char *tem = comname;
  1251.  
  1252.   validate_comname (comname);
  1253.  
  1254.   c = lookup_cmd (&tem, cmdlist, "", 0, 1);
  1255.  
  1256.   if (c->class != (int) class_user)
  1257.     error ("Command \"%s\" is built-in.", comname);
  1258.  
  1259.   if (from_tty)
  1260.     printf ("Type documentation for \"%s\".\n\
  1261. End with a line saying just \"end\".\n", comname);
  1262.  
  1263.   doclines = read_command_lines ();
  1264.  
  1265.   if (c->doc) free (c->doc);
  1266.  
  1267.   {
  1268.     register struct command_line *cl1;
  1269.     register int len = 0;
  1270.  
  1271.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1272.       len += strlen (cl1->line) + 1;
  1273.  
  1274.     c->doc = (char *) xmalloc (len + 1);
  1275.     *c->doc = 0;
  1276.  
  1277.     for (cl1 = doclines; cl1; cl1 = cl1->next)
  1278.       {
  1279.     strcat (c->doc, cl1->line);
  1280.     if (cl1->next)
  1281.       strcat (c->doc, "\n");
  1282.       }
  1283.   }
  1284.  
  1285.   free_command_lines (&doclines);
  1286. }
  1287.  
  1288. static void
  1289. print_gdb_version ()
  1290. {
  1291.   printf ("GDB %s, Copyright (C) 1989 Free Software Foundation, Inc.\n\
  1292. There is ABSOLUTELY NO WARRANTY for GDB; type \"info warranty\" for details.\n\
  1293. GDB is free software and you are welcome to distribute copies of it\n\
  1294.  under certain conditions; type \"info copying\" to see the conditions.\n",
  1295.       version);
  1296. }
  1297.  
  1298. static void
  1299. version_info ()
  1300. {
  1301.   immediate_quit++;
  1302.   print_gdb_version ();
  1303.   immediate_quit--;
  1304. }
  1305.  
  1306. /* xgdb calls this to reprint the usual GDB prompt.  */
  1307.  
  1308. void
  1309. print_prompt ()
  1310. {
  1311.   printf ("%s", prompt);
  1312.   fflush (stdout);
  1313. }
  1314.  
  1315. /* Command to specify a prompt string instead of "(gdb) ".  */
  1316.  
  1317. static void
  1318. set_prompt_command (text)
  1319.      char *text;
  1320. {
  1321.   char *p, *q;
  1322.   register int c;
  1323.   char *new;
  1324.  
  1325.   if (text == 0)
  1326.     error_no_arg ("string to which to set prompt");
  1327.  
  1328.   new = (char *) xmalloc (strlen (text) + 2);
  1329.   p = text; q = new;
  1330.   while (c = *p++)
  1331.     {
  1332.       if (c == '\\')
  1333.     {
  1334.       /* \ at end of argument is used after spaces
  1335.          so they won't be lost.  */
  1336.       if (*p == 0)
  1337.         break;
  1338.       c = parse_escape (&p);
  1339.       if (c == 0)
  1340.         break; /* C loses */
  1341.       else if (c > 0)
  1342.         *q++ = c;
  1343.     }
  1344.       else
  1345.     *q++ = c;
  1346.     }
  1347.   if (*(p - 1) != '\\')
  1348.     *q++ = ' ';
  1349.   *q++ = '\0';
  1350.   new = (char *) xrealloc (new, q - new);
  1351.   free (prompt);
  1352.   prompt = new;
  1353. }
  1354.  
  1355. static void
  1356. quit_command ()
  1357. {
  1358.   if (have_inferior_p ())
  1359.     {
  1360.       if (query ("The program is running.  Quit anyway? "))
  1361.     {
  1362.       /* Prevent any warning message from reopen_exec_file, in case
  1363.          we have a core file that's inconsistent with the exec file.  */
  1364.       exec_file_command (0, 0);
  1365.       kill_inferior ();
  1366.     }
  1367.       else
  1368.     error ("Not confirmed.");
  1369.     }
  1370.   /* Save the history information if it is appropriate to do so.  */
  1371.   if (write_history_p && history_filename)
  1372.     write_history (history_filename);
  1373.   exit (0);
  1374. }
  1375.  
  1376. int
  1377. input_from_terminal_p ()
  1378. {
  1379.   return instream == stdin;
  1380. }
  1381.  
  1382. static void
  1383. pwd_command (arg, from_tty)
  1384.      char *arg;
  1385.      int from_tty;
  1386. {
  1387.   if (arg) error ("The \"pwd\" command does not take an argument: %s", arg);
  1388.   getwd (dirbuf);
  1389.  
  1390.   if (strcmp (dirbuf, current_directory))
  1391.     printf ("Working directory %s\n (canonically %s).\n",
  1392.         current_directory, dirbuf);
  1393.   else
  1394.     printf ("Working directory %s.\n", current_directory);
  1395. }
  1396.  
  1397. static void
  1398. cd_command (dir, from_tty)
  1399.      char *dir;
  1400.      int from_tty;
  1401. {
  1402.   int len;
  1403.   int change;
  1404.  
  1405.   if (dir == 0)
  1406.     error_no_arg ("new working directory");
  1407.  
  1408.   dir = tilde_expand (dir);
  1409.   make_cleanup (free, dir);
  1410.  
  1411.   len = strlen (dir);
  1412.   dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
  1413.   if (dir[0] == '/')
  1414.     current_directory = dir;
  1415.   else
  1416.     {
  1417.       current_directory = concat (current_directory, "/", dir);
  1418.       free (dir);
  1419.     }
  1420.  
  1421.   /* Now simplify any occurrences of `.' and `..' in the pathname.  */
  1422.  
  1423.   change = 1;
  1424.   while (change)
  1425.     {
  1426.       char *p;
  1427.       change = 0;
  1428.  
  1429.       for (p = current_directory; *p;)
  1430.     {
  1431.       if (!strncmp (p, "/./", 2)
  1432.           && (p[2] == 0 || p[2] == '/'))
  1433.         strcpy (p, p + 2);
  1434.       else if (!strncmp (p, "/..", 3)
  1435.            && (p[3] == 0 || p[3] == '/')
  1436.            && p != current_directory)
  1437.         {
  1438.           char *q = p;
  1439.           while (q != current_directory && q[-1] != '/') q--;
  1440.           if (q != current_directory)
  1441.         {
  1442.           strcpy (q-1, p+3);
  1443.           p = q-1;
  1444.         }
  1445.         }
  1446.       else p++;
  1447.     }
  1448.     }
  1449.  
  1450.   if (chdir (dir) < 0)
  1451.     perror_with_name (dir);
  1452.  
  1453.   if (from_tty)
  1454.     pwd_command ((char *) 0, 1);
  1455. }
  1456.  
  1457. static void
  1458. source_command (arg, from_tty)
  1459.      char *arg;
  1460.      int from_tty;
  1461. {
  1462.   FILE *stream;
  1463.   struct cleanup *cleanups;
  1464.   char *file = arg;
  1465.  
  1466.   if (file == 0)
  1467.     /* Let source without arguments read .gdbinit.  */
  1468.     file = ".gdbinit";
  1469.  
  1470.   file = tilde_expand (file);
  1471.   make_cleanup (free, file);
  1472.  
  1473.   stream = fopen (file, "r");
  1474.   if (stream == 0)
  1475.     perror_with_name (file);
  1476.  
  1477.   cleanups = make_cleanup (source_cleanup, instream);
  1478.  
  1479.   instream = stream;
  1480.  
  1481.   command_loop ();
  1482.  
  1483.   do_cleanups (cleanups);
  1484. }
  1485.  
  1486. static void
  1487. echo_command (text)
  1488.      char *text;
  1489. {
  1490.   char *p = text;
  1491.   register int c;
  1492.  
  1493.   if (text)
  1494.     while (c = *p++)
  1495.       {
  1496.     if (c == '\\')
  1497.       {
  1498.         /* \ at end of argument is used after spaces
  1499.            so they won't be lost.  */
  1500.         if (*p == 0)
  1501.           return;
  1502.  
  1503.         c = parse_escape (&p);
  1504.         if (c >= 0)
  1505.           fputc (c, stdout);
  1506.       }
  1507.     else
  1508.       fputc (c, stdout);
  1509.       }
  1510. }
  1511.  
  1512. static void
  1513. dump_me_command ()
  1514. {
  1515.   if (query ("Should GDB dump core? "))
  1516.     {
  1517.       signal (SIGQUIT, SIG_DFL);
  1518.       kill (getpid (), SIGQUIT);
  1519.     }
  1520. }
  1521.  
  1522. int
  1523. parse_binary_operation (caller, arg)
  1524.      char *caller, *arg;
  1525. {
  1526.   int length;
  1527.  
  1528.   if (!arg || !*arg)
  1529.     return 1;
  1530.  
  1531.   length = strlen (arg);
  1532.  
  1533.   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
  1534.     length--;
  1535.  
  1536.   if (!strncmp (arg, "on", length)
  1537.       || !strncmp (arg, "1", length)
  1538.       || !strncmp (arg, "yes", length))
  1539.     return 1;
  1540.   else
  1541.     if (!strncmp (arg, "off", length)
  1542.     || !strncmp (arg, "0", length)
  1543.     || !strncmp (arg, "no", length))
  1544.       return 0;
  1545.     else
  1546.       error ("\"%s\" not given a binary valued argument.", caller);
  1547. }
  1548.  
  1549. /* Functions to manipulate command line editing control variables.  */
  1550.  
  1551. static void
  1552. set_editing (arg, from_tty)
  1553.      char *arg;
  1554.      int from_tty;
  1555. {
  1556.   command_editing_p = parse_binary_operation ("set command-editing", arg);
  1557. }
  1558.  
  1559. /* Number of commands to print in each call to editing_info.  */
  1560. #define Hist_print 10
  1561. static void
  1562. editing_info (arg, from_tty)
  1563.      char *arg;
  1564.      int from_tty;
  1565. {
  1566.   /* Index for history commands.  Relative to history_base.  */
  1567.   int offset;
  1568.  
  1569.   /* Number of the history entry which we are planning to display next.
  1570.      Relative to history_base.  */
  1571.   static int num = 0;
  1572.  
  1573.   /* The first command in the history which doesn't exist (i.e. one more
  1574.      than the number of the last command).  Relative to history_base.  */
  1575.   int hist_len;
  1576.  
  1577.   struct _hist_entry {
  1578.     char *line;
  1579.     char *data;
  1580.   } *history_get();
  1581.   extern int history_base;
  1582.  
  1583.   printf_filtered ("Interactive command editing is %s.\n",
  1584.       command_editing_p ? "on" : "off");
  1585.  
  1586.   printf_filtered ("History expansion of command input is %s.\n",
  1587.       history_expansion_p ? "on" : "off");
  1588.   printf_filtered ("Writing of a history record upon exit is %s.\n",
  1589.       write_history_p ? "enabled" : "disabled");
  1590.   printf_filtered ("The size of the history list (number of stored commands) is %d.\n",
  1591.       history_size);
  1592.   printf_filtered ("The name of the history record is \"%s\".\n\n",
  1593.       history_filename ? history_filename : "");
  1594.  
  1595.   /* Print out some of the commands from the command history.  */
  1596.   /* First determine the length of the history list.  */
  1597.   hist_len = history_size;
  1598.   for (offset = 0; offset < history_size; offset++)
  1599.     {
  1600.       if (!history_get (history_base + offset))
  1601.     {
  1602.       hist_len = offset;
  1603.       break;
  1604.     }
  1605.     }
  1606.  
  1607.   if (arg)
  1608.     {
  1609.       if (arg[0] == '+' && arg[1] == '\0')
  1610.     /* "info editing +" should print from the stored position.  */
  1611.     ;
  1612.       else
  1613.     /* "info editing <exp>" should print around command number <exp>.  */
  1614.     num = (parse_and_eval_address (arg) - history_base) - Hist_print / 2;
  1615.     }
  1616.   /* "info editing" means print the last Hist_print commands.  */
  1617.   else
  1618.     {
  1619.       num = hist_len - Hist_print;
  1620.     }
  1621.  
  1622.   if (num < 0)
  1623.     num = 0;
  1624.  
  1625.   /* If there are at least Hist_print commands, we want to display the last
  1626.      Hist_print rather than, say, the last 6.  */
  1627.   if (hist_len - num < Hist_print)
  1628.     {
  1629.       num = hist_len - Hist_print;
  1630.       if (num < 0)
  1631.     num = 0;
  1632.     }
  1633.  
  1634.   if (num == hist_len - Hist_print)
  1635.     printf_filtered ("The list of the last %d commands is:\n\n", Hist_print);
  1636.   else
  1637.     printf_filtered ("Some of the stored commands are:\n\n");
  1638.  
  1639.   for (offset = num; offset < num + Hist_print && offset < hist_len; offset++)
  1640.     {
  1641.       printf_filtered ("%5d  %s\n", history_base + offset,
  1642.           (history_get (history_base + offset))->line);
  1643.     }
  1644.  
  1645.   /* The next command we want to display is the next one that we haven't
  1646.      displayed yet.  */
  1647.   num += Hist_print;
  1648.   
  1649.   /* If the user repeats this command with return, it should do what
  1650.      "info editing +" does.  This is unnecessary if arg is null,
  1651.      because "info editing +" is not useful after "info editing".  */
  1652.   if (from_tty && arg)
  1653.     {
  1654.       arg[0] = '+';
  1655.       arg[1] = '\0';
  1656.     }
  1657. }
  1658.  
  1659. static void
  1660. set_history_expansion (arg, from_tty)
  1661.      char *arg;
  1662.      int from_tty;
  1663. {
  1664.   history_expansion_p = parse_binary_operation ("set history expansion", arg);
  1665. }
  1666.  
  1667. static void
  1668. set_history_write (arg, from_tty)
  1669.      char *arg;
  1670.      int from_tty;
  1671. {
  1672.   write_history_p = parse_binary_operation ("set history write", arg);
  1673. }
  1674.  
  1675. static void
  1676. set_history (arg, from_tty)
  1677.      char *arg;
  1678.      int from_tty;
  1679. {
  1680.   printf ("\"set history\" must be followed by the name of a history subcommand.\n");
  1681.   help_list (sethistlist, "set history ", -1, stdout);
  1682. }
  1683.  
  1684. static void
  1685. set_history_size (arg, from_tty)
  1686.      char *arg;
  1687.      int from_tty;
  1688. {
  1689.   if (!*arg)
  1690.     error_no_arg ("set history size");
  1691.  
  1692.   history_size = atoi (arg);
  1693. }
  1694.  
  1695. static void
  1696. set_history_filename (arg, from_tty)
  1697.      char *arg;
  1698.      int from_tty;
  1699. {
  1700.   int i;
  1701.  
  1702.   if (!arg)
  1703.     error_no_arg ("history file name");
  1704.   
  1705.   arg = tilde_expand (arg);
  1706.   make_cleanup (free, arg);
  1707.  
  1708.   i = strlen (arg) - 1;
  1709.   
  1710.   free (history_filename);
  1711.   
  1712.   while (i > 0 && (arg[i] == ' ' || arg[i] == '\t'))
  1713.     i--;
  1714.  
  1715.   if (!*arg)
  1716.     history_filename = (char *) 0;
  1717.   else
  1718.     history_filename = savestring (arg, i + 1);
  1719.   history_filename[i] = '\0';
  1720. }
  1721.  
  1722. int info_verbose;
  1723.  
  1724. static void
  1725. set_verbose_command (arg, from_tty)
  1726.      char *arg;
  1727.      int from_tty;
  1728. {
  1729.   info_verbose = parse_binary_operation ("set verbose", arg);
  1730. }
  1731.  
  1732. static void
  1733. verbose_info (arg, from_tty)
  1734.      char *arg;
  1735.      int from_tty;
  1736. {
  1737.   if (arg)
  1738.     error ("\"info verbose\" does not take any arguments.\n");
  1739.   
  1740.   printf ("Verbose printing of information is %s.\n",
  1741.       info_verbose ? "on" : "off");
  1742. }
  1743.  
  1744. static void
  1745. float_handler ()
  1746. {
  1747.   error ("Invalid floating value encountered or computed.");
  1748. }
  1749.  
  1750.  
  1751. static void
  1752. initialize_cmd_lists ()
  1753. {
  1754.   cmdlist = (struct cmd_list_element *) 0;
  1755.   infolist = (struct cmd_list_element *) 0;
  1756.   enablelist = (struct cmd_list_element *) 0;
  1757.   disablelist = (struct cmd_list_element *) 0;
  1758.   deletelist = (struct cmd_list_element *) 0;
  1759.   enablebreaklist = (struct cmd_list_element *) 0;
  1760.   setlist = (struct cmd_list_element *) 0;
  1761.   sethistlist = (struct cmd_list_element *) 0;
  1762.   unsethistlist = (struct cmd_list_element *) 0;
  1763. }
  1764.  
  1765. static void
  1766. initialize_main ()
  1767. {
  1768.   char *tmpenv;
  1769.   /* Command line editing externals.  */
  1770.   extern int (*rl_completion_entry_function)();
  1771.   extern char *rl_completer_word_break_characters;
  1772.  
  1773.   /* Set default verbose mode on.  */
  1774.   info_verbose = 1;
  1775.  
  1776. #ifndef KGDB
  1777.   prompt = savestring ("(gdb) ", 6);
  1778. #else
  1779.   prompt = savestring ("(kgdb) ", 7);
  1780. #endif
  1781.   /* Set the important stuff up for command editing.  */
  1782.   command_editing_p = 1;
  1783.   history_expansion_p = 0;
  1784.   write_history_p = 0;
  1785.   
  1786.   if (tmpenv = getenv ("HISTSIZE"))
  1787.     history_size = atoi (tmpenv);
  1788.   else
  1789.     history_size = 256;
  1790.  
  1791.   stifle_history (history_size);
  1792.  
  1793.   if (tmpenv = getenv ("GDBHISTFILE"))
  1794.     history_filename = savestring (tmpenv, strlen(tmpenv));
  1795.   else
  1796.     /* We include the current directory so that if the user changes
  1797.        directories the file written will be the same as the one
  1798.        that was read.  */
  1799.     history_filename = concat (current_directory, "/.gdb_history", "");
  1800.  
  1801.   read_history (history_filename);
  1802.  
  1803.   /* Setup important stuff for command line editing.  */
  1804.   rl_completion_entry_function = (int (*)()) symbol_completion_function;
  1805.   rl_completer_word_break_characters = gdb_completer_word_break_characters;
  1806.  
  1807.   /* Define the classes of commands.
  1808.      They will appear in the help list in the reverse of this order.  */
  1809.  
  1810.   add_cmd ("obscure", class_obscure, 0, "Obscure features.", &cmdlist);
  1811.   add_cmd ("alias", class_alias, 0, "Aliases of other commands.", &cmdlist);
  1812.   add_cmd ("user", class_user, 0, "User-defined commands.\n\
  1813. The commands in this class are those defined by the user.\n\
  1814. Use the \"define\" command to define a command.", &cmdlist);
  1815.   add_cmd ("support", class_support, 0, "Support facilities.", &cmdlist);
  1816.   add_cmd ("status", class_info, 0, "Status inquiries.", &cmdlist);
  1817.   add_cmd ("files", class_files, 0, "Specifying and examining files.", &cmdlist);
  1818.   add_cmd ("breakpoints", class_breakpoint, 0, "Making program stop at certain points.", &cmdlist);
  1819.   add_cmd ("data", class_vars, 0, "Examining data.", &cmdlist);
  1820.   add_cmd ("stack", class_stack, 0, "Examining the stack.\n\
  1821. The stack is made up of stack frames.  Gdb assigns numbers to stack frames\n\
  1822. counting from zero for the innermost (currently executing) frame.\n\n\
  1823. At any time gdb identifies one frame as the \"selected\" frame.\n\
  1824. Variable lookups are done with respect to the selected frame.\n\
  1825. When the program being debugged stops, gdb selects the innermost frame.\n\
  1826. The commands below can be used to select other frames by number or address.",
  1827.        &cmdlist);
  1828.   add_cmd ("running", class_run, 0, "Running the program.", &cmdlist);
  1829.  
  1830.   add_com ("pwd", class_files, pwd_command,
  1831.        "Print working directory.  This is used for your program as well.");
  1832.   add_com ("cd", class_files, cd_command,
  1833.        "Set working directory to DIR for debugger and program being debugged.\n\
  1834. The change does not take effect for the program being debugged\n\
  1835. until the next time it is started.");
  1836.  
  1837.   add_cmd ("prompt", class_support, set_prompt_command,
  1838.        "Change gdb's prompt from the default of \"(gdb)\"",
  1839.        &setlist);
  1840.   add_com ("echo", class_support, echo_command,
  1841.        "Print a constant string.  Give string as argument.\n\
  1842. C escape sequences may be used in the argument.\n\
  1843. No newline is added at the end of the argument;\n\
  1844. use \"\\n\" if you want a newline to be printed.\n\
  1845. Since leading and trailing whitespace are ignored in command arguments,\n\
  1846. if you want to print some you must use \"\\\" before leading whitespace\n\
  1847. to be printed or after trailing whitespace.");
  1848.   add_com ("document", class_support, document_command,
  1849.        "Document a user-defined command.\n\
  1850. Give command name as argument.  Give documentation on following lines.\n\
  1851. End with a line of just \"end\".");
  1852.   add_com ("define", class_support, define_command,
  1853.        "Define a new command name.  Command name is argument.\n\
  1854. Definition appears on following lines, one command per line.\n\
  1855. End with a line of just \"end\".\n\
  1856. Use the \"document\" command to give documentation for the new command.\n\
  1857. Commands defined in this way do not take arguments.");
  1858.  
  1859.   add_com ("source", class_support, source_command,
  1860.        "Read commands from a file named FILE.\n\
  1861. Note that the file \".gdbinit\" is read automatically in this way\n\
  1862. when gdb is started.");
  1863.   add_com ("quit", class_support, quit_command, "Exit gdb.");
  1864.   add_com ("help", class_support, help_command, "Print list of commands.");
  1865.   add_com_alias ("q", "quit", class_support, 1);
  1866.   add_com_alias ("h", "help", class_support, 1);
  1867.  
  1868.   add_cmd ("verbose", class_support, set_verbose_command,
  1869.        "Change the number of informational messages gdb prints.",
  1870.        &setlist);
  1871.   add_info ("verbose", verbose_info,
  1872.         "Status of gdb's verbose printing option.\n");
  1873.  
  1874.   add_com ("dump-me", class_obscure, dump_me_command,
  1875.        "Get fatal error; make debugger dump its core.");
  1876.  
  1877.   add_cmd ("editing", class_support, set_editing,
  1878.        "Enable or disable command line editing.\n\
  1879. Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\
  1880. Without an argument, command line editing is enabled.", &setlist);
  1881.  
  1882.   add_prefix_cmd ("history", class_support, set_history,
  1883.           "Generic command for setting command history parameters.",
  1884.           &sethistlist, "set history ", 0, &setlist);
  1885.  
  1886.   add_cmd ("expansion", no_class, set_history_expansion,
  1887.        "Enable or disable history expansion on command input.\n\
  1888. Without an argument, history expansion is enabled.", &sethistlist);
  1889.  
  1890.   add_cmd ("write", no_class, set_history_write,
  1891.        "Enable or disable saving of the history record on exit.\n\
  1892. Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\
  1893. Without an argument, saving is enabled.", &sethistlist);
  1894.  
  1895.   add_cmd ("size", no_class, set_history_size,
  1896.        "Set the size of the command history, \n\
  1897. ie. the number of previous commands to keep a record of.", &sethistlist);
  1898.  
  1899.   add_cmd ("filename", no_class, set_history_filename,
  1900.        "Set the filename in which to record the command history\n\
  1901.  (the list of previous commands of which a record is kept).", &sethistlist);
  1902.  
  1903.   add_prefix_cmd ("info", class_info, info_command,
  1904.           "Generic command for printing status.",
  1905.           &infolist, "info ", 0, &cmdlist);
  1906.   add_com_alias ("i", "info", class_info, 1);
  1907.  
  1908.   add_info ("editing", editing_info, "Status of command editor.");
  1909.  
  1910.   add_info ("version", version_info, "Report what version of GDB this is.");
  1911. }
  1912. @
  1913.  
  1914.  
  1915. 1.2
  1916. log
  1917. @Mips doesn't support core files yet
  1918. @
  1919. text
  1920. @d1739 1
  1921. a1739 1
  1922.   
  1923. d1743 1
  1924. d1745 3
  1925. a1747 1
  1926.  
  1927. @
  1928.  
  1929.  
  1930. 1.1
  1931. log
  1932. @Initial revision
  1933. @
  1934. text
  1935. @d368 3
  1936. d372 1
  1937. d412 3
  1938. d416 1
  1939. @
  1940.